package indi.yangshenhui.spring.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public abstract class BaseDao<T> {
	private static final Logger LOGGER = Logger.getLogger(BaseDao.class);

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public abstract Class<T> getPojoClass();

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	/**
	 * 增、删、改
	 * 
	 * @param sql
	 * @return
	 */
	public int update(String sql) {
		return this.getJdbcTemplate().update(sql);
	}

	/**
	 * 增、删、改
	 * 
	 * @param sql
	 * @return
	 */
	public int[] update(String[] sql) {
		return this.getJdbcTemplate().batchUpdate(sql);
	}

	/**
	 * 查
	 * 
	 * @param sql
	 * @param objects
	 * @return
	 */
	public List<T> queryList(String sql, Object[] objects) {
		LOGGER.info(sql);
		List<T> tlist = jdbcTemplate.query(sql, objects,
				new BeanPropertyRowMapper<T>(this.getPojoClass()));
		return tlist;
	}

	/**
	 * 查
	 * 
	 * @param sql
	 * @return
	 */
	public T queryObject(String sql) {
		return this.getJdbcTemplate().queryForObject(sql, this.getPojoClass());
	}

	/**
	 * 查
	 * 
	 * @param sql
	 * @return
	 */
	public int queryCount(String sql) {
		return this.getJdbcTemplate().queryForObject(sql, Integer.class);
	}

}